import 'package:flutter/material.dart';
import 'package:flutter_rider/main/widgets/ext/u_layout.dart';
import 'package:flutter_rider/main/widgets/ext/u_text.dart';
import 'package:flutter_rider/main/widgets/ext/u_title_layout.dart';
import 'package:flutter_rider/main/widgets/state/state_layout.dart';
import 'package:flutter_rider/utils/screen_fit.dart';
import 'package:flutter_svg/svg.dart';
import 'package:flutter_rider/generated/r.dart';
import 'package:flutter_rider/main/widgets/ui_constant.dart';
import 'package:flutter_rider/event/refresh_order_event.dart';
import 'package:flutter_rider/main.dart';
import 'package:flutter_rider/utils/open_utils.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../utils/tip.dart';
import 'copy_order_page.dart';

class ConfirmOrderSuccessPage extends StatefulWidget {
  int type;
  String id;
  ConfirmOrderSuccessPage(this.type,this.id);

  @override
  State<StatefulWidget> createState() {
    return ConfirmOrderSuccessPageState();
  }
}

class ConfirmOrderSuccessPageState extends State<ConfirmOrderSuccessPage> {
  String appBarTitle = "新增运单";
  dynamic _data;
  @override
  void initState() {
    super.initState();
    appBarTitle = widget.type == 1?"确认运单":"新增运单";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: UIColor.color_main,
      appBar: UTitleLayout.getBackTitle(context, appBarTitle),
      body: StateLayout(
        url: "/waybill/detail?waybill_no=${widget.id}",
        childBuilder: _renderContent,
        dataCallback: (data) {
          _data = data;
        },
      ).padding(EdgeInsets.only(left: MediaQuery.of(context).padding.left, right: MediaQuery.of(context).padding.right)),
    );
  }
  // Widget build(BuildContext context) {
  //   return Material(
  //       color: UIColor.color_main,
  //       child: Column(
  //           children: [
  //             _renderTitle(),
  //             _renderTop(),
  //             120.hspace,
  //             _renderButton()
  //           ]
  //       )
  //   );
  // }

  Widget _renderContent(dynamic data) {
    return Column(
        children: [
        //_renderTitle(),
        _renderTop(),
        120.hspace,
        _renderButton()
        ]
    );
  }

  // Widget _renderTitle() {
  //   return Container(
  //     color: UIColor.color_content,
  //     width: double.infinity,
  //     height: MediaQuery.of(context).padding.top + 44.rpx,
  //     padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
  //     child:Center(
  //         child: UText.get(appBarTitle, Colors.black, 16.rpx, fw: FontWeight.w600)
  //     )
  //   );
  // }
  
  Widget _renderTop() {
    String name = appBarTitle + "提交成功";
    return Column(
      children: [
        120.hspace,
        SvgPicture.asset(R.image_ok,width: 56.rpx,height: 56.rpx),
        24.hspace,
        UText.get(name, Colors.black, 20.rpx, fw: FontWeight.w600),
        8.hspace,
        UText.get("正在为您分配车辆，请稍后～", UIColor.color_t7, 14.rpx),
      ],
    );
  }

  Widget _renderButton() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Container(
          decoration: BoxDecoration(
              border: Border.all(width: 1.rpx,color: const Color(0xFFDCDCDC)),
              borderRadius: const BorderRadius.all(Radius.circular(12.0))
          ),
          child: UText.getBkButton("复制运单", 14.rpx,
              width: 156.rpx,
              height: 48.rpx,
              bkColor: UIColor.color_content,
              color: UIColor.color_t13,
              radius: 12.rpx,
              onPressed: () async{
                var _sp = await SharedPreferences.getInstance();
                bool _showCopyWaybillOrder = _sp.getBool("_showCopyWaybillOrder") as bool;
                if(_showCopyWaybillOrder) {
                  OpenUtils.open(context, CopyOrderPage(_data));
                } else {
                  Tip.show("当前没有“复制运单”的权限，无法操作");
                }
              }
          ),
        ),
        16.wspace,
        UText.getBkButton("返回列表", 14.rpx,
            width: 156.rpx,
            height: 48.rpx,
            bkColor: UIColor.color_primary,
            color: UIColor.color_content,
            radius: 12.rpx,
            onPressed: () {
              Navigator.of(context).popUntil((route) => route.isFirst);
              eventBus.fire(RefreshOrderEvent());
            }
        ),
      ],
    );
  }
}